/*
** Copyright (C) 2010 Calin Radoni
**
** This file is part of CHScanner.
**
** CHScanner is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** CHScanner is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with CHScanner. If not, see <http://www.gnu.org/licenses/>.
*/

#include "StdAfx.h"
#include ".\marpcacherec.h"

//---------------------------------------------------------------------------

MArpCacheRec::MArpCacheRec(void)
{
	Initialize();
}

MArpCacheRec::~MArpCacheRec(void)
{
}

//---------------------------------------------------------------------------

void MArpCacheRec::Initialize(void)
{
	v6 = false;
	addrIP.Initialize();
	addrIPv6.Initialize();
	addrMAC.Initialize();
}

void MArpCacheRec::LoadFromPointer(MArpCacheRec* recIn)
{
	Initialize();
	if(recIn==NULL) return;

	v6 = recIn->v6;
	addrIP.LoadFromPointer(&recIn->addrIP);
	addrIPv6.LoadFromPointer(&recIn->addrIPv6);
	addrMAC.LoadFromPointer(&recIn->addrMAC);
}

//---------------------------------------------------------------------------

CString MArpCacheRec::ToString(void)
{
	CString str;

	if(v6) str.Format(_T("%s %s"), addrMAC.GetS(), addrIPv6.GetS());
	else   str.Format(_T("%s %s"), addrMAC.GetS(), addrIP.GetS());

	return str;
}

bool MArpCacheRec::FromString(CString strIn)
{
	CString str, sss;
	bool res;
	int pos;

	Initialize();

	str = strIn.Trim();
	if(str.GetLength()<26) return false; // MAC + ' ' + IPv6

	sss = str.Left(17);
	res = addrMAC.LoadFromString(sss);
	if(!res) return false;

	res = true;

	str = str.Mid(18);
	pos = str.Find(':');
	if(pos==-1){
		// IPv4
		v6 = false;
		addrIP.Set(str);
		if(addrIP.GetD()==0)
			res = false;
	}
	else{
		// IPv6
		v6 = true;
		res = addrIPv6.SetS(str);
		if(addrIPv6.IsEmpty())
			res = false;
	}

	return true;
}

//---------------------------------------------------------------------------
